package com.small.os.minio.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.small.os.minio.entity.AqFile;
import com.small.os.minio.mapper.AqFileMapper;
import com.small.os.minio.service.IAqFileService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件关联表 服务实现类
 * </p>
 *
 * @author wangguochao
 * @since 2020-10-21
 */
@Service

public class AqFileServiceImpl extends ServiceImpl<AqFileMapper, AqFile> implements IAqFileService {

    @Override
    public List<String> getObjAssociationFile(String proId) {
        LambdaQueryWrapper<AqFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AqFile::getProId, proId);
        List<AqFile> list = this.list(wrapper);
        return list.stream().map(AqFile::getFileUrl).collect(Collectors.toList());
    }

    @Override
    public List<AqFile> getObjAssociationFile(Set<String> proId) {
        if (proId == null || proId.isEmpty()) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<AqFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AqFile::getProId, proId);
        return this.list(wrapper);
    }

    @Override
    public void addImg(List<String> imgList, String proId) {
        this.rem(proId);
        if (imgList == null || imgList.isEmpty()) {
            return;
        }
        ArrayList<AqFile> list = new ArrayList<>();
        for (String s : imgList) {
            AqFile aqFile = new AqFile();
            aqFile.setFileUrl(s);
            aqFile.setProId(proId);
            list.add(aqFile);
        }
        this.saveBatch(list);
    }

    private void rem(String proId) {
        LambdaQueryWrapper<AqFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AqFile::getProId, proId);
        this.remove(wrapper);
    }
}
